package net.plankton.agents.main;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import net.plankton.agents.Agent;

public class ThreadedOutput implements SurveillanceOutput {

	SurveillanceOutput out;
	
	private Queue<Message> queue = new ConcurrentLinkedQueue<Message>();
	
	public ThreadedOutput(SurveillanceOutput out) {
		this.out = out;
		new OutputThread().start();
	}
	
	
	public void output(Agent a,String output) {
		queue.offer(new Message(a,output));

	}

	
	class OutputThread extends Thread {
		@Override
		public void run() {
			while (true) {
				try {
					Message val = queue.poll();
					if (val!=null)
						out.output(val.getAgent(),val.getMessage());
					Thread.sleep(500);
				}
				catch (Exception e) {
					
				}
			}
		}
	}
	
	class Message {
		private Agent a;
		private String mess;

		public Message(Agent a,String mess) {
			this.a=a;
			this.mess=mess;
		}
		
		public Agent getAgent() {
			return a;
		}
		public String getMessage() {
			return mess;
		}
	}
	
}
